﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class Notifications : ClassManager.SessionManagerPage
    {
        private void GetAllNotifications()
        {
            NotificationsGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.NotificationsProvider.GetAll();
        }

        private GPSTrackingDBV2.Entities.Notifications Invoke_Entity(Hashtable Entity)
        {
            GPSTrackingDBV2.Entities.Notifications NotificationsEntity = new GPSTrackingDBV2.Entities.Notifications();
            NotificationsEntity.NotificationId = Convert.ToInt32(Entity[GPSTrackingDBV2.Entities.NotificationsColumn.NotificationId.ToString()]);
            NotificationsEntity.Name = Convert.ToString(Entity[GPSTrackingDBV2.Entities.NotificationsColumn.Name.ToString()]);

            return NotificationsEntity;

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.NotificationsColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.NotificationsGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
            }
        }

        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Notificaciones...');", true);
        }

        protected void NotificationsGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetAllNotifications();
            NotificationsGrid.Height = Unit.Parse(HiddenFieldHeight.Value.ToString());
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }

        protected void NotificationsGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.Notifications NotificationsEntity = Invoke_Entity(Entity);
                NotificationsEntity.NotificationId = GPSTrackingDBV2Custom.Data.DataRepository.NotificationsProvider.GetNextID();

                GPSTrackingDBV2.Data.DataRepository.NotificationsProvider.Insert(NotificationsEntity);
            }
            catch (Exception ex)
            {
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);            
                showErrorMessage("No se puede ingresar la notificación.");
            }
        }

        protected void NotificationsGrid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.Notifications NotificationsEntity = Invoke_Entity(Entity);
                NotificationsEntity.OriginalNotificationId = NotificationsEntity.NotificationId;

                GPSTrackingDBV2.Data.DataRepository.NotificationsProvider.Update(NotificationsEntity);
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref NotificationsGrid);
                showErrorMessage("No se puede actualizar la notificación porque esta asociado a un otros registros.");
            }
        }

        protected void NotificationsGrid_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.Notifications NotificationsEntity = Invoke_Entity(Entity);

                GPSTrackingDBV2.Data.DataRepository.NotificationsProvider.Delete(NotificationsEntity);
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref NotificationsGrid);
                showErrorMessage("No se puede eliminar la notificación porque esta asociado a un otros registros.");
            }
        }

        protected void UsersGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.OwnerTableView.IsItemInserted && e.Item.IsInEditMode) //item is about to be inserted  
            {
                TextBox txtid;
                GridEditableItem insertItem = (GridEditableItem)e.Item;
                txtid = (TextBox)insertItem["NotificationId"].Controls[0];
                txtid.ReadOnly = false;
            }
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            NotificationsGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in NotificationsGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }
            NotificationsGrid.MasterTableView.Rebind();
        }
    }
}